#include "dominion.h"
#include "dominion_helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

void zzzadventurercardplay(int drawntreasure, int currentPlayer, int cardDrawn,
		int temphand[MAX_HAND], int z, struct gameState* state);

int test()
{
	struct gameState g;
	struct gameState * game = &g;
	memset(game, 0, sizeof(struct gameState));
	
	//initialize game
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		sea_hag, tribute, smithy};
	int numPlayers = 2 + rand()%3;	
	initializeGame(numPlayers, k, time(NULL), game);
	int drawntreasure = 0;
	int cardDrawn = rand()%26;
	int z = 0;
	
	//randomize game state
	int handCount = rand()%MAX_HAND;	
	int deckCount = rand()%(MAX_DECK - handCount);
	int discardCount = MAX_DECK - deckCount;
	int currentPlayer = rand()%numPlayers;
	int temphand[MAX_HAND];
	
	game->deckCount[currentPlayer] = deckCount;
	game->handCount[currentPlayer] = handCount;
	game->discardCount[currentPlayer] = discardCount;
	
	for (int i = 0; i < deckCount; i++)
		game->deck[currentPlayer][i] = rand()%26;		
	for (int j = 0; j < handCount; j++)
		game->hand[currentPlayer][j] = rand()%26;		
	for (int k = 0; k < discardCount; k++)
		game->discard[currentPlayer][k] = rand()%26;
	
	zzzadventurercardplay(drawntreasure, currentPlayer, cardDrawn, temphand, z, game);
	
	if ((game->handCount[currentPlayer] == handCount + 2) && 
			(game->hand[currentPlayer][game->handCount[currentPlayer] - 1] == 
				copper || silver || gold) && 
			(game->hand[currentPlayer][game->handCount[currentPlayer] - 2] == 
				copper || silver || gold))
		return 0;
	else {
		printf("zzzadventurercardplay(): FAIL when test drawing two treasure cards\n");
		printf("Parameter values:\n");
		printf("drawntreasure: %d, currentPlayer: %d, cardDrawn: %d, z: %d\n", drawntreasure, currentPlayer, cardDrawn, z);
		printf("Local variables:\n");
		printf("handCount: %d\n", handCount);
		printf("Game state: \n");
		printf("deckCount: %d, handCount: %d, discardCount: %d, top card: %d, prior card: %d\n", 
				game->deckCount[currentPlayer], game->handCount[currentPlayer], game->discardCount[currentPlayer], 
				game->hand[currentPlayer][game->handCount[currentPlayer] - 1],
				game->hand[currentPlayer][game->handCount[currentPlayer] - 2]);
		return -1;
	}
}

int main(int argc, char *argv[])
{
	int i;
	srand(time(NULL));
	
	for (i = 0; i < 10000; i++) {
		if (test() == -1)
			break;
	}
	
	if (i == 10000)
		printf("zzzadventurercardplay(): PASS random test drawing two treasure cards\n");
	
	return 0;
}
